package com.ht.module.bus.controller;

import com.ht.module.bus.entity.TTransportationCompanyStatistics;
import com.ht.module.bus.service.TTransportationCompanyStatisticsService;
import com.ht.util.JsonResult;
import com.ht.util.Pager;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
 * <p>
 * 车企承运信息填报表 前端控制器
 * </p>
 *
 * @author hejialun
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/t-transportation-company-statistics")
public class TTransportationCompanyStatisticsController {
    @Resource
    private TTransportationCompanyStatisticsService transportationCompanyStatisticsService;


    @ApiOperation("分页查询")
    @GetMapping("/findPage")
    public JsonResult findPage(Pager<TTransportationCompanyStatistics> pager, TTransportationCompanyStatistics en) {
        return JsonResult.success(transportationCompanyStatisticsService.findPage(pager, en));
    }

    @ApiOperation("导入excel")
    @PostMapping("/import")
    public JsonResult importExcel(@RequestParam("file") MultipartFile file) {
        transportationCompanyStatisticsService.importExcel(file);
        return JsonResult.success();
    }


    @ApiOperation("下载导入模板")
    @GetMapping("/downloadTemplate")
    public ResponseEntity<Object> downloadTemplate() throws Exception  {
        // 指向resources目录下的文件
        ClassPathResource resource = new ClassPathResource("templates/车企承运信息填报表.xlsx");
        String fileName = resource.getFilename();

        // 检查文件是否存在
        if (!resource.exists()) {
            return ResponseEntity.notFound().build();
        }

        // 对文件名进行URL编码（注意：这里可能需要根据实际情况调整编码方式）
        String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        // 使用filename*参数（如果浏览器支持）和filename参数作为回退
        // 注意：filename*参数需要指定字符集（这里是UTF-8），但并非所有浏览器都支持
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFileName + "\"; filename*=UTF-8''" + encodedFileName);
        headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
        headers.add(HttpHeaders.PRAGMA, "no-cache");
        headers.add(HttpHeaders.EXPIRES, "0");

        // 返回文件
        return ResponseEntity.ok()
                .headers(headers)
                .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .body(resource);

    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/delete-by-id/{id}")
    public JsonResult delete(@PathVariable(value = "id") String id) {
        transportationCompanyStatisticsService.removeById(id);
        return JsonResult.success();
    }


}
